package com.stormrage.edu.serv.edu.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduMajorDao;
import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.exam.ExmScorePercentServ;
import com.stormrage.edu.serv.exam.ExmScoreServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.exam.ExmScore;
import com.stormrage.entity.exam.ExmScorePercent;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.rep.LeaMajorStudyRep;
import com.stormrage.entity.learn.req.LeaMajorSelectReq;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduMajorServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 专业 Service
 * @author www.stormragetech.com
 * @date 2016年4月18日
 * @version V1.0
 */
@Service
public class EduMajorServImpl implements EduMajorServ {

	private static final Logger logger = LoggerFactory.getLogger(EduLecServImpl.class);

	@Autowired
	private EduMajorDao eduMajorDao;

	@Autowired
	private ExmScoreServ exmScoreServ;

	@Autowired
	private ExmScorePercentServ exmScorePercentServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Override
	public Integer queryCountByProjectId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryListById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		Integer count = eduMajorDao.queryCountByProjectId(map);

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryListById 】-【 COMPLETED 】");
		return count;

	}

	@Override
	public EduMajor queryById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduMajor entity = eduMajorDao.queryById(map);

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EduMajor> queryListById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryListById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduMajor> array = eduMajorDao.queryListById(map);

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 QueryListById 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForStudy(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForStudy 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0);

		List<LeaMajorStudyRep> array = eduMajorDao.queryListForStudy(map);
		int seq = 1;

		Iterator<LeaMajorStudyRep> it = array.iterator();
		while (it.hasNext()) {
			LeaMajorStudyRep entity = it.next();
			entity = LeaMajorStudyRep.instance(entity, seq);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForStudy 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForExam(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForExam 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0);

		List<LeaMajorStudyRep> array = eduMajorDao.queryListForExam(map);
		int seq = 1;

		Iterator<LeaMajorStudyRep> it = array.iterator();
		while (it.hasNext()) {

			LeaMajorStudyRep entity = it.next();

			LeaStudentReq req = LeaStudentReq.instance(arg0, null, entity.getId());
			LeaStudent stu = leaStudentServ.queryByAcctMajor(req);

			ExmScore score = exmScoreServ.queryByStuId(stu.getId());
			ExmScorePercent perc = exmScorePercentServ.queryByTermId(entity.getTermId());

			Integer total = score.getExam() * perc.getExam() / 100 + score.getOper() * perc.getOper() / 100
					+ score.getUsua() * perc.getUsua() / 100;
			entity = LeaMajorStudyRep.instance(entity, seq, score.getExam(), score.getOper(), score.getUsua(),
					perc.getExam(), perc.getOper(), perc.getUsua(), total);

			seq++;

		}

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForExam 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForSelect(final LeaMajorSelectReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForSelect 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("selected", arg0.getSelected());

		List<LeaMajorStudyRep> array = eduMajorDao.queryListForSelect(map);
		int seq = 1;

		Iterator<LeaMajorStudyRep> it = array.iterator();
		while (it.hasNext()) {
			LeaMajorStudyRep entity = it.next();
			entity = LeaMajorStudyRep.instance(entity, seq);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 EduMajorServ 】-【 queryListForSelect 】-【 COMPLETED 】");
		return array;

	}

}
